package com.study;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;

/**
 * @创建时间 2020/5/7 14:14
 */
public class SignIn {

    private static final String USER_NAME = " ";
    private static final String USER_PWD = " ";
    private static final String REFERRER_URL = "http://www.wode5.com/";
    private static final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.34 Safari/537.36 Edg/83.0.478.25";
    private static final String HOST = "www.wode5.com";
    //登录URL
    private static String URL_LOGIN = "http://www.wode5.com/e/member/doaction.php";
    //签到 URL
    private static final String URL_Sign_in = "http://www.wode5.com/e/dongpo/qiandao/do.php?enews=1";
    //领取全勤 奖励
    private static final String URL_Sign_ALL = "http://www.wode5.com/e/dongpo/qiandao/do.php?enews=2";


    //签到成功后的cookie
    private static Map<String, String> cookies;
    private static Connection connection;

    static {
        System.out.println("---------签到------------");
        connection = Jsoup.connect(URL_LOGIN).userAgent(userAgent)
                .timeout(10 * 1000)
                .method(Connection.Method.POST)
                .data("username", USER_NAME).data("password", USER_PWD)
                .data("enews", "login").data("ecmsfrom", "").data("lifetime", "0")
                //  // 字段参数| 文件名称| 文件流
                .header("Connection", "keep-alive")
                .followRedirects(true);

        connection.header("Host", HOST)
                .header("Referer", REFERRER_URL)
                .header("Origin", REFERRER_URL);


        //Host: www.wode5.com
        //Origin: http://www.wode5.com


        Connection.Response execute = null;
        try {
            execute = connection.ignoreContentType(true).execute();
            System.out.println(execute.body());
        } catch (IOException e) {
            e.printStackTrace();
        }
        cookies = execute.cookies();
        System.out.println("登录cookie");
        System.out.println(cookies);
    }

    /**
     * jsoup 模拟签到
     *
     * @param args
     */
    public static void main(String[] args) throws IOException {
        signForScore(connection, cookies, "1");
        signForScore(connection, cookies, "2");

    }

    /**
     * 签到领取积分
     *
     * @param connection
     * @param cookies
     * @param type       1 签到 2 领取全勤
     * @throws IOException
     */
    private static void signForScore(Connection connection, Map<String, String> cookies, String type) throws IOException {
        Connection.Response response = connection.url(URL_Sign_in).method(Connection.Method.POST)
                .data("enews", type)
                .cookies(cookies)
                .execute();

        System.out.println(response.body());
        System.out.println("------------");
    }


}
